package com.theta.gateway.biz.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.stereotype.Service;

import com.theta.gateway.biz.service.DynamicRouteService;
import com.theta.gateway.infra.common.exception.BusinessException;
import com.theta.gateway.infra.common.exception.enums.ErrorEnum;
import com.theta.gateway.infra.common.utils.JsonUtil;
import com.theta.gateway.infra.routecfg.model.converter.RouteDTOConverter;
import com.theta.gateway.infra.routecfg.model.dataobject.response.RouteCfgDO;
import com.theta.gateway.infra.routecfg.model.dataobject.response.RouteInfoDO;
import com.theta.gateway.infra.routecfg.service.GatewayRouteRepository;
import com.theta.gateway.infra.routecfg.service.RouteCfgRepository;

import lombok.extern.slf4j.Slf4j;

/**
 * 路由操作类
 * @author theta
 *
 */
@Slf4j
@Service
public class DynamicRouteServiceImpl implements DynamicRouteService {

	@Autowired
	private GatewayRouteRepository gatewayRouteRepository;
	@Autowired
	private RouteCfgRepository routeCfgRepository;
	 
	
	
	@Override
	public void addRouteInfo(RouteInfoDO dto) {
		try {
//			RouteInfoDTO dto= BizBOConverter.convertRouteRequest2DTO(request);
			log.info("{}",JsonUtil.toJsonString(dto));
			this.gatewayRouteRepository.addRoute(dto,true);
		} catch (Exception e) {
			log.error("saveRouteInfo",e);
			throw new BusinessException(ErrorEnum.ROUTE_GATEWAY_SAVE_ERROR,e.getMessage());
		}

	}

	@Override
	public void updateRouteInfo(RouteInfoDO dto) {
		try {
			this.gatewayRouteRepository.updateRoute(dto);
		} catch (Exception e) {
			log.error("updateRouteInfo",e);
			throw new BusinessException(ErrorEnum.ROUTE_GATEWAY_UPDATE_ERROR,e.getMessage());
		}

	}

	@Override
	public void deleteRouteInfo(String routeId) {
		try {
			RouteInfoDO route=this.getRouteInfo(routeId);
			if(route==null) {
				log.warn("路由ID[{}]在网关内存中不存在",routeId);
				return;
			}
			this.gatewayRouteRepository.delete(routeId,true);
		} catch (NotFoundException e) {
			throw new BusinessException(ErrorEnum.ROUTE_GATEWAY_ROUTE_ID_NOTEXIST,routeId);
		} catch (Exception e) {
			log.error("deleteRouteInfo",e);
			throw new BusinessException(ErrorEnum.ROUTE_GATEWAY_DELETE_ERROR,e.getMessage());
		}
	}
	@Override
	public RouteInfoDO getRouteInfo(String routeId) {
		List<RouteInfoDO> routes= this.gatewayRouteRepository.getAllRoute()
		.stream().filter(p->p.getRouteId().equals(routeId))
		.collect(Collectors.toList());
		return CollectionUtils.isEmpty(routes)?null:routes.get(0);
	}

	@Override
	public List<RouteInfoDO> queryRouteList() {
		 
		return null;
	}

	@Override
	public List<RouteInfoDO> queryAllRoute() {
		return this.gatewayRouteRepository.getAllRoute();
	}

	@Override
	public void refreshRouteCfgs(List<RouteCfgDO> cfgList){
		try {
			List<String> routeIds= this.queryAllRoute().stream().map(RouteInfoDO::getRouteId).collect(Collectors.toList());
			this.gatewayRouteRepository.deleteBatch(routeIds, false);
			//将数据库中的路由信息更新至网关中
			List<RouteInfoDO> routes=cfgList.stream().map(cfg->RouteDTOConverter.convertCfg2InfoDo(cfg)).collect(Collectors.toList());
			this.gatewayRouteRepository.addRoutes(routes);
		} catch (Exception e) {
			log.error("refreshAllRoutes",e);
			throw new BusinessException(ErrorEnum.ROUTE_GATEWAY_REFRESH_ERROR,e.getMessage());
		}
	}
	
	

}
